package cn.com.sbell.vss.monitor.getter;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.sbell.vss.monitor.IPerformanceDataGetter;

public abstract class AbstractPerformanceDataGetter implements
		IPerformanceDataGetter {
	private static final Log log = LogFactory
			.getLog(AbstractPerformanceDataGetter.class);

	public static String executeCommand(String command) {
		if (log.isDebugEnabled()) {
			log.debug("Execute command: " + command);
		}
		Process p = null;
		try {
			p = Runtime.getRuntime().exec(command);

			BufferedReader br = new BufferedReader(new InputStreamReader(p
					.getInputStream()));
			StringBuffer result = new StringBuffer();

			while (true) {
				int i = br.read();
				if (i < 0) {
					break;
				}
				result.append((char) i);
			}
			br.close();
			String s = result.toString();
			if (log.isDebugEnabled()) {
				log.debug("Execute result: " + s);
			}
			return s;
		} catch (IOException ex) {
			log.warn("Error occured while executing command:" + command, ex);
		} finally {
			if (p != null) {
				p.destroy();
			}
		}
		return null;
	}
	public static int getIndex(String line,String name)
	{
		line = line.trim();
		while (line.indexOf("  ") >= 0) {
			line = line.replaceAll("  ", " ");
		}
		String[] cols = line.split(" ");
		int len=cols.length;
		for(int i=-1;i>=-len;i--)
		{
			if(cols[len+i].contains(name))
			{
				return i;
			}
		}
		return -1;
	}
	public static String getRow(String lines, int lineIndex) {

		BufferedReader br = new BufferedReader(new InputStreamReader(
				new ByteArrayInputStream(lines.getBytes())));

		List lineList = new ArrayList();
		try {
			while (true) {
				String line = br.readLine();
				if (line == null) {
					break;
				}
				lineList.add(line);
			}
			br.close();
		} catch (IOException ex) {
			log.warn(ex);
		}
		String line = null;
		if (lineIndex < 0) {
			line = (String) lineList.get(lineList.size() + lineIndex);
		} else if (lineIndex == 0) {
			line = (String) lineList.get(0);
		} else {
			line = (String) lineList.get(lineIndex - 1);
		}
		if (log.isDebugEnabled()) {
			log.debug("Line " + lineIndex + " is " + line);
		}
		return line;
	}

	public static int getRowCount(String lines) {
		BufferedReader br = new BufferedReader(new InputStreamReader(
				new ByteArrayInputStream(lines.getBytes())));
		String line = null;
		List lineList = new ArrayList();
		try {
			while (true) {
				line = br.readLine();
				if (line == null) {
					break;
				}
				lineList.add(line);
			}
			br.close();
		} catch (IOException ex) {
			log.warn(ex);
		}
		return lineList.size();
	}

	public static String getColumn(String line, int columnIndex) {
		line = line.trim();
		while (line.indexOf("  ") >= 0) {
			line = line.replaceAll("  ", " ");
		}
		String[] cols = line.split(" ");
		// for (int i = 0; i < cols.length; i++) {
		// System.out.println(cols[i]);
		// }
		String col = null;
		if (columnIndex < 0) {
			col = cols[columnIndex + cols.length];
		} else if (columnIndex == 0) {
			col = cols[0];
		} else {
			col = cols[columnIndex - 1];
		}
		if (log.isDebugEnabled()) {
			log.debug("Column " + columnIndex + " is " + col);
		}
		return col;
	}
}
